import {defineConfig, loadEnv} from 'vite'
import vue from '@vitejs/plugin-vue'
import topLevelAwait from 'vite-plugin-top-level-await'
import { resolve } from 'path'
import viteCompression from 'vite-plugin-compression';//Gzip
import { visualizer } from "rollup-plugin-visualizer";
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons' // 【svg-icons相关】
import VitePluginHtmlEnv from 'vite-plugin-html-env'

const time = new Date().getTime()
export default defineConfig(({mode, command}) => {
  const env = loadEnv(mode, process.cwd())
  const { VITE_BASE } = env
  return {
    define: {
      'process.env': process.env
    },
    plugins: [
      vue(),
      viteCompression(),
      VitePluginHtmlEnv(),
      visualizer(),
      createSvgIconsPlugin({
        // 指定需要缓存的图标文件夹
        iconDirs: [resolve(process.cwd(), 'src/assets/svg')],
        // 指定symbolId格式
        symbolId: 'icon-[dir]-[name]',
        // svgoOptions: {
        //   plugins: [
        //     {
        //       name: 'removeAttrs',
        //       params: {
        //         attrs: ['class', 'data-name', 'fill', 'stroke']
        //       }
        //     }
        //   ]
        // }
      }),
      topLevelAwait({
        promiseExportName: '__tla',
        promiseImportName: i => `__tla_${i}`
      })
    ],
    base: '/' + VITE_BASE + '/',
    resolve: {
      alias: {
        '@': resolve(__dirname, 'src'),
      },
    },
    server: {
      port: 888,
      host: '0.0.0.0',
      open: true,
      strictPort: false,
      proxy: {
      }
    },
    build: {
      outDir: "visual-style",
      rollupOptions: {//分包优化
        output: {
          manualChunks(id) {
            if (id.includes('node_modules')) {
              return time + id.toString().split('node_modules/')[1].split('/')[0].toString();
            } else {
              return time + id.toString();
            }
          }
        }
      }
    },
    publicDir: 'src/out',
    optimizeDeps: {
      include: []
    }
  } as any
})
